在 Rust 的世界中,编译器是你的第一道防线,能在问题发生前就捕获内存泄漏和类型不匹配。然而,编译器无法理解你的 意图。这时, 自动化测试 便作为逻辑正确性的“安全网”登场。
1. 相辅相成的组合
虽然类型系统负责保证结构的完整性,但测试则验证功能行为。在一项 库项目中,编译器会确保你不会将字符串传给数学函数,但只有测试才能确保 $f(x) = y$ 能得出预期结果。
2. 标准生命周期
Rust 测试遵循严格的三步流程:
- 准备阶段: 初始化数据(例如,创建一个库实例)。
- 执行阶段: 执行被测试的具体逻辑。
- 断言阶段: 使用类似
assert_eq!的宏来验证状态。
集成的工具链支持 文档测试 (文档中的可运行示例)以及 基准测试 以确保你的代码始终保持极速运行。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which attribute must be placed above a function to mark it as a test for the runner?
#[cfg(test)]#[test]#[verify]#[cargo::test]✅ Correct!
The #[test] attribute is the specific metadata that signals to the compiler to include the function in the test runner binary.❌ Incorrect
While #[cfg(test)] is used for modules, individual functions require the #[test] attribute.QUESTION 2
How does Rust primarily signal that a test has failed?
The function returns
false.The function executes a
return Error;.The function thread panics.
The compiler issues a warning.
✅ Correct!
In Rust, a test fails if the logic triggers a panic (usually via assertions).❌ Incorrect
Rust tests look for panics to identify failure, though they can also return a Result.QUESTION 3
Why is
#[cfg(test)] used on the tests module?To make the tests run faster.
To ensure tests are only compiled when running
cargo test.To allow the module to access private variables.
To export the tests to other crates.
✅ Correct!
This conditional compilation attribute ensures your production binary doesn't include the testing code, keeping it lean.❌ Incorrect
It is a configuration hint that prevents test code from leaking into the 'cargo build' output.QUESTION 4
Which command both compiles and executes the test runner binary?
cargo build --testcargo testrustc test.rscargo run tests✅ Correct!
cargo test is the unified command that handles building the test binary and running it automatically.❌ Incorrect
cargo build only compiles; cargo test is needed to execute the results.QUESTION 5
What is the key difference between Doc-tests and Unit tests?
Doc-tests are only for documentation and cannot fail.
Unit tests live in code files; Doc-tests live inside triple-backtick comments.
Doc-tests are written in HTML, not Rust.
There is no difference.
✅ Correct!
Doc-tests are a unique Rust feature where code examples in your documentation comments are compiled and tested as if they were source code.❌ Incorrect
Doc-tests are actual Rust code embedded in comments to ensure README/documentation examples remain valid.Inventory Logic Shield
Case Study: Library Management System
You are developing a library management crate. While the type system ensures you don't enter a negative number for initial inventory, it cannot prevent a logic bug where borrowing a book accidentally increments the stock instead of decrementing it.
Q
1. What would be the 'Setup' phase for testing a 'borrow_book' function?
Solution:
Creating a new instance of the library struct and populating it with at least one book in the inventory (e.g., initial count = 5).
Creating a new instance of the library struct and populating it with at least one book in the inventory (e.g., initial count = 5).
Q
2. Which assertion macro would you use to verify the inventory after the 'Run' phase?
Solution:
assert_eq!(library.stock_count(), 4); This compares the actual count after execution with the expected decremented value.